package br.com.tartarugasindomaveis.ti.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {
	private static final String DATABASE_NAME = "tartaruga.db";
	private static final int DATABASE_VERSION = 2;
	private static final String TABLE_NAME = "agenda";

	private Context context;
	private SQLiteDatabase db;

	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into "
			+ TABLE_NAME
			+ "(titulo,quando,onde,status,descricao,foto) values (?, ? ,? , ?, ?, ?)";

	public DataHelper(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
	}

	public long insert(String titulo, String quando, String onde,
			String status, String descricao, String fotos) {
		this.insertStmt.bindString(1, titulo);
		this.insertStmt.bindString(2, quando);
		this.insertStmt.bindString(3, onde);
		this.insertStmt.bindString(4, status);
		this.insertStmt.bindString(5, descricao);
		this.insertStmt.bindString(6, fotos);
		return this.insertStmt.executeInsert();
	}

	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}

	public List<Agenda> selectAll(String where) {
		List<Agenda> list = new ArrayList<Agenda>();
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "titulo",
				"quando", "onde", "status", "descricao", "foto" }, where, null,
				null, null, "quando asc");
		if (cursor.moveToFirst()) {
			do {
				list.add(cursorToAgenda(cursor));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return list;
	}

	public Agenda getAgenda(String titulo) {
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "titulo",
				"quando", "onde", "status", "descricao", "foto" }, "titulo = '"
				+ titulo + "'", null, null, null, null);
		cursor.moveToFirst();
		return cursorToAgenda(cursor);
	}

	private Agenda cursorToAgenda(Cursor cursor) {
		Agenda agenda = new Agenda(cursor.getString(0), cursor.getString(1),
				cursor.getString(2), cursor.getString(3), cursor.getString(4),
				cursor.getString(5));
		return agenda;
	}

	private static class OpenHelper extends SQLiteOpenHelper {
		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE "
					+ TABLE_NAME
					+ "(id INTEGER PRIMARY KEY, titulo TEXT, quando TEXT, onde TEXT, status TEXT, descricao TEXT, foto TEXT)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("Example",
					"Upgrading database, this will drop tables and recreate.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
}
